package com.zyz.demo.service;

import com.zyz.demo.mapper.BlogMapper;
import com.zyz.demo.mapper.UserMapper;
import com.zyz.demo.model.BlogInfo;
import com.zyz.demo.model.UserInfo;
import com.zyz.demo.utils.SecretUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
@Slf4j
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private BlogMapper blogMapper;

    public UserInfo login(String userName) {
        return userMapper.selectUserPasswordByName(userName);
    }

    public UserInfo getUserInfo(Integer userId) {
        return userMapper.selectUserInfoByUserId(userId);
    }

    public UserInfo getAuthorInfo(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectBlogInfoByBlogId(blogId);
        Assert.notNull(blogInfo,"查询文章为空");
        return userMapper.selectUserInfoByUserId(blogInfo.getUserId());
    }

    public int getUserBlogSize(Integer userId) {
        return userMapper.getUserBlogSize(userId);
    }

    public int register(UserInfo userInfo) {
        return userMapper.addUserInfo(userInfo);
    }

    public String secret(String password) {
        return SecretUtils.encrypt(password);
    }

    public boolean checkIndexExist(String userName) {
        int result = userMapper.checkIndexExist(userName);
        log.info("result = " + result);
        if (0 == result) {
            return false;
        }
        return true;
    }
}
